import {createSlice, PayloadAction} from '@reduxjs/toolkit'
import {AddressComponent} from '@/type/position'
import {RootState} from '@/store/store'


interface State {
  address_component: AddressComponent
}

const initialState: State = {
  address_component: {
    nation: '中国',
    province: '重庆市',
    city: '重庆市',
    district: '合川区'
  }
}

export const positionSlice = createSlice({
  name: 'position',
  initialState,
  reducers: {
    setAddressComponent (state, action: PayloadAction<AddressComponent>) {
      Object.assign(state.address_component, action.payload)
    }
  }
})

export const {setAddressComponent} = positionSlice.actions

// 根状态
export const selectPosition = (root: RootState) => root

export default positionSlice.reducer
